package gggameserver;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.connection.channel.direct.Session.Command;
import net.schmizz.sshj.userauth.keyprovider.KeyProviderUtil;
import net.schmizz.sshj.userauth.keyprovider.OpenSSHKeyFile;
import net.schmizz.sshj.userauth.keyprovider.PKCS8KeyFile;

import gggameserver.compiler.GGSolutionData;
import gggameserver.dbaccess.GGDBAccess;
import gggameserver.dbaccess.GGMySqlDBAccess;
import gggameserver.dbaccess.GGTestDBAccess;
import gggameserver.exceptions.GGTimeout;
import gggameserver.games.connection.GGConnection;
import gggameserver.games.connection.ProcessConnection;
import gggameserver.testbed.GGMirroredConnection;
import gggameserver.testbed.GGMirroredTestBed;
import gggameserver.testbed.GGTestBed;
import gggameserver.testbed.GGTestBedFactory;

public class GGGameServerTest {
	
	public static void testPutty() throws GGTimeout, IOException {
        SSHClient ssh = new SSHClient();
        ssh.addHostKeyVerifier("64:53:7b:82:d1:6b:54:ad:66:b0:ae:ed:d3:b5:1d:f3");

        ssh.connect("192.168.92.147");
        try {
        	ssh.authPassword("jane", "j");

            Command cmd = ssh.startSession().exec("bash -c \"'/usr/lib/jvm/java-6-openjdk/jre/bin/java' 'Program'\"");
            //Command cmd = ssh.startSession().exec("bash -c \"'/usr/lib/jvm/java-6-openjdk/jre/bin/java' '-version'\"");
            
            BufferedReader r = new BufferedReader(new InputStreamReader(cmd.getInputStream()));
            
            System.out.println("|");
            System.out.println("|" + r.readLine());
            System.out.println("|" + r.readLine());
            System.out.println("|" + r.readLine());
            System.out.println("|" + r.readLine());

            System.out.print(cmd.getOutputAsString());
            System.out.println("\n** exit status: " + cmd.getExitStatus());

        } finally {
            ssh.disconnect();
        }
		
//		ProcessConnection p = new ProcessConnection("putty -i data/gg_key.ppk ggi@192.168.92.147", new File("."));
//		String line = null;
//		while (null != (line = p.receiveCommand(-1))) 
//			System.out.println(">>" + line);
//		p.close();
	}

	public static void testPlayLahing() throws IOException {
		GGTestBedFactory testbed_fac = GGTestBedFactory.createTestBedFactory();
		
		String gameConfPath = "C:/prog/manguvoistlus/svn/trunk/battle/server/java/ggameserver-lahing2011";
		String path = "C:/prog/manguvoistlus/svn/trunk/battle/server/java/ggameserver-lahing2011/bin";
		String path2 = "C:/prog/manguvoistlus/svn/trunk/battle/server/java/gggameserver/testbed/_test/cpp1";
		
		GGTestBed tb1 = testbed_fac.prepareLocalTestTestBed(new File(path), "java");
		GGTestBed tb2 = testbed_fac.prepareLocalCompiledTestBed(new File(path), "java");
		
		GGGameServer.playLahing(new File(gameConfPath), tb1, tb2, new PrintWriter(System.out) );
	}
	
	public static void testPlayLahingFull() throws Exception {
		GGTestBedFactory testbed_fac = GGTestBedFactory.createTestBedFactory();
		
		//String test_cpp = "test/1-C++.zip";
		String test_cpp = "test/av-cpp1.zip";
		
//		GGMirroredTestBed tb1 = testbed_fac.prepareMirroredTestBed(
//				"compile", new GGSolutionData("C++", test_cpp), 1);
		GGMirroredTestBed tb1 = testbed_fac.prepareMirroredTestBed(
				"compile", new GGSolutionData("FreePascal", "test/av-fpc1.zip"), 1);
		System.out.println();
		System.out.println(">>>>>>>>>>>>>>>>>>>>>>");
		System.out.println(tb1.isValid());
		System.out.println(tb1.getLog());
		GGMirroredTestBed tb2 = testbed_fac.prepareMirroredTestBed(
				"compile", new GGSolutionData("java", "l2011_example/j1.zip"), 2);
		
//		GGConnection con1 = compiling_testbed1.createConnection();
//		GGConnection con2 = compiling_testbed1.createConnection(true);
//System.out.println("xxx");
//		System.out.println(con1.receiveCommand(-1));
//System.out.println("yyy");
//		System.out.println(con2.receiveCommand(-1));
		
		String gameConfPath = "C:/prog/manguvoistlus/svn/trunk/battle/server/java/ggameserver-lahing2011";
		String path = "C:/prog/manguvoistlus/svn/trunk/battle/server/java/ggameserver-lahing2011/bin";
		GGGameServer.playLahing(new File(gameConfPath), tb1, tb2, new PrintWriter(System.out) );
//		testbed_fac.prepareMirroredTestBed("compile", new GGSolutionData("java", "l2011_example/j2.zip"));
		
//		String gameConfPath = "C:/prog/manguvoistlus/svn/trunk/battle/server/java/ggameserver-lahing2011";
//		String path = "C:/prog/manguvoistlus/svn/trunk/battle/server/java/ggameserver-lahing2011/bin";
//		
//		GGTestBed tb1 = testbed_fac.prepareLocalCompiledTestBed(new File(path), "java");
//		GGTestBed tb2 = testbed_fac.prepareLocalCompiledTestBed(new File(path), "java");
//		
//		GGGameServer.playLahing(new File(gameConfPath), tb1, tb2, new PrintWriter(System.out) );
	}
	
	public static void testServer() throws Exception {
		GGTestDBAccess dbaccess = new GGTestDBAccess();
		//GGDBAccess dbaccess = new GGTestDBAccess();
		GGGameServer server = new GGGameServer(dbaccess);
		//server.testCompileProgram(4);
		System.out.println("Play game --> " + server.playGame());
	}
	
	public static void main(String[] args) throws Exception {
		//testPutty();
		//testPlayLahingFull();
		testServer();
	}
	
}
